package com.woniuxy.controllers;

import com.woniuxy.entitys.RestPOJO;
import com.woniuxy.entitys.Role;
import com.woniuxy.services.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "职位管理接口")
public class RoleController {
    @Autowired
    private RoleService roleService;


    @ApiOperation(
            value = "查询所有职位",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //查询所有职位/角色
    @GetMapping("/roles/all")
    public RestPOJO getAllRoles(){
        List<Role> roleList= roleService.getAllRoles();
        RestPOJO restPOJO = new RestPOJO();
        if(roleList != null){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(roleList);

        }else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }

        return restPOJO;
    }

    @ApiOperation(
            value = "根据查询语句查询职位",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="key",value="查询类型",required=false),
            @ApiImplicitParam(paramType="query",name="value",value="查询内容",required=false),
    })
    //根据查询语句查询职位/角色
    @GetMapping("/roles/key")
    public RestPOJO getRolesByKey(String key , String value){
        System.out.println(key + "$$$" +value);
        List<Role> roleList= roleService.getRolesByKey(key,value);
        RestPOJO restPOJO = new RestPOJO();
        if(roleList != null){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(roleList);

        }else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }

        return restPOJO;
    }

    @ApiOperation(
            value = "根据role_id查询权限",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="role_id",value="职位id",required=true),
    })
    //根据role_id查询
    @GetMapping("/roles/{role_id}")
    public RestPOJO getUserByUid(@PathVariable("role_id") Integer role_id){
        System.out.println(role_id);
        Role role = roleService.getRoleByRid(role_id);
        RestPOJO restPOJO = new RestPOJO();
        if(role != null){
            restPOJO.setMessage("登录成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(role);

        }else{
            restPOJO.setMessage("登录失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;

    }

    @ApiOperation(
            value = "保存添加职位",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //保存添加职位/角色
    @PostMapping("/roles")
    public RestPOJO saveAddRole(@RequestBody Role role){

        RestPOJO restPOJO = null;
        try {
            restPOJO = new RestPOJO();
            Integer count = roleService.addRole(role) ;

            if(count > 0){
                restPOJO.setMessage("添加成功");
                restPOJO.setStatus(1000);

            }else{
                restPOJO.setMessage("添加失败");
                restPOJO.setStatus(1001);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restPOJO.setMessage("添加失败，职位编码不能重复");
            restPOJO.setStatus(1002);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "保存修改职位",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //保存修改职位角色
    @PutMapping("/roles")
    public RestPOJO saveUpdUser(@RequestBody Role role){
        //System.out.println(users);
        RestPOJO restPOJO = null;
        try {
            restPOJO = new RestPOJO();
            Integer count = roleService.updRole(role) ;

            if(count > 0){
                restPOJO.setMessage("保存修改成功");
                restPOJO.setStatus(1000);

            }else{
                restPOJO.setMessage("保存修改失败");
                restPOJO.setStatus(1001);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restPOJO.setMessage("保存修改失败，职位编码不能重复");
            restPOJO.setStatus(1002);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "通过role_id删除一个职位",
            httpMethod = "DELETE",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="role_id",value="职位id",required=true),
    })
    //通过role_id删除一个职位
    @DeleteMapping("/roles/{role_id}")
    public RestPOJO delRoleByRid(@PathVariable("role_id") Integer role_id){
        //System.out.println(users);
        //获取员工状态为正常或停职的role_id ,若传入的参数和其中相等，则不允许删除
        RestPOJO restPOJO = null;
        try {
            Integer count = roleService.delRoleByRid(role_id) ;
            restPOJO = new RestPOJO();
            if(count > 0){
                restPOJO.setMessage("删除成功");
                restPOJO.setStatus(1000);

            }else{
                restPOJO.setMessage("删除失败");
                restPOJO.setStatus(1001);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restPOJO.setMessage("删除失败！这个职位有人正在使用,无法删除!");
            restPOJO.setStatus(1002);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "批量删除职位，数据格式如下[88,99]",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //批量删除职位
    @PostMapping("/roles/deletelist")
    public RestPOJO delSomeRoles(@RequestBody Object[] checkedIds){
        //System.out.println(checkedIds.toString());
        RestPOJO restPOJO = null;
        try {
            restPOJO = new RestPOJO();
            Integer count = roleService.delSomeRoles(checkedIds) ;

            if(count > 0){
                restPOJO.setMessage("批量删除成功");
                restPOJO.setStatus(1000);

            }else{
                restPOJO.setMessage("批量删除失败");
                restPOJO.setStatus(1001);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restPOJO.setMessage("批量删除失败！这个职位有人正在使用,无法批量删除!");
            restPOJO.setStatus(1002);
        }
        return restPOJO;
    }
}
